'use strict';

angular.module('app')
	.controller('menuController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
	                                function($rootScope, $scope, $http, $state,$timeout) {
		$scope.title = '菜单管理';
        $scope.param = { };
        $scope.loading = false;

		$scope.search = function () {
	        $scope.loading = true;
            $scope.param.menuType = $("#menuType").val();
			$.ajax({
				url : $scope.app.project_name+'/menu/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}

		$scope.searchRe = function () {
	        $scope.loading = true;
	        $scope.param.pageNum = 1;
			$.ajax({
				url : $scope.app.project_name+'/menu/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
                    $(".editSortBtn").show();
                    $(".saveSortBtn").hide();
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}

		$scope.changeMenuType = function () {
			var menuType=$scope.param.menuType;
			if( menuType == 1 ){
				$("#menuBox").hide();
			}else{
                $("#menuBox").show();
                $.ajax({
                    url : $scope.app.project_name+ '/menu/selParentNameList',
                    data: {'id': menuType}
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        $scope.parentList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                });
			}
            $scope.searchRe();
		}

		$scope.changeParent = function () {
			var parentId=$scope.param.parentId;
			console.log(parentId);
				$.ajax({
					url : $scope.app.project_name+ '/menu/selParentNameList',
					data: {'id': parentId}
				}).then(function(result) {
					if (result.httpCode == 200) {
						$scope.parentList = result.data;
					} else {
						$scope.msg = result.msg;
					}
				});
			$scope.searchRe();
		}

		// 编辑排序
		$scope.editSort=function ($event) {

            if ($('#menuType').val() == 1){
                if(!$('#menuType').val()){
                    $scope.alertModal({'content':'类型不能为空'});
                    return;
                }
                if($('#menuName').val()){
                    $scope.alertModal({'content':'名称为空方可编辑排序'});
                    return;
                }
			}else{
                if(!$('#parentId').val()){
                    $scope.alertModal({'content':'上级菜单不能为空'});
                    return;
                }
                if($('#menuName').val()){
                    $scope.alertModal({'content':'名称为空方可编辑排序'});
                    return;
                }

			}
            $("#menuList").sortable();
            $("#menuList").disableSelection();

			for (var i = 0; i < $scope.pageInfo.records.length; i++) {
				var tr = $(".sort  tbody tr")[i];
				$($(tr).find("td")[4]).hide();
				$($(tr).find("td")[5]).hide();
				$(".editSortBtn").hide();
                $("#sortId").hide();
				$(".saveSortBtn").show();
			}
		}



		$scope.saveSort=function ($event) {
			var flag=true;
			var batchUpdates = new Array();//声明一个数组来保存多个的id值
			var sortArray = new Array();//声明一个数组来保存多个序号的值
			for (var j = 0; j < $scope.pageInfo.records.length; j++) {
				// var tr = $(".sort  tbody tr")[j];
				// var id = $($(tr).find("td")[0]).html();
				// var td = $($(tr).find("td")[5]);
				// var sort = j+1;
				// sortArray[j]=sort;//用来存放序号
				// var batchUpdate = id + "-" + sort;
				// batchUpdates[j] = batchUpdate;

                var tr = $(".sort  tbody tr")[j];

                var id = $($(tr).find("td")[1]).html();
                var sort = j+1;
                sortArray[j]=sort;//用来存放序号
                var batchUpdate = id + "-" + sort;
                batchUpdates[j] = batchUpdate;

				// if (!isNaN(sort) && sort > 0 && sort <= $scope.pageInfo.records.length) {
                // 	continue;
                // } else {
                // 	$scope.alertModal({"content": "数据格式输入有误或序号大于了现有的最大的条数"});
                // 	flag = false;
                // }
			}
			// var s = sortArray;//用来存放序号的值
            // var count=0;
            // for(var m=0;m<s.length;m++) {//验证是否有重复的。如果有的话就加一
            // 	if (s[m] == s[m + 1]) {
            // 		count++;
            // 	}
            // }
            // if(count!=0){
				// $scope.alertModal({"content": "序号重复，请重新排序"});
				// flag = false;
            // }
			if (flag) {
				$.ajax({
					url: $scope.app.project_name + '/menu/editSort',
					data: {'ids': batchUpdates},
					traditional: true
				}).then(function (result) {
					console.log(result);
					if (result.httpCode == 200) {
						$scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $("#sortId").show();
                        $(".saveSortBtn").hide();
                        $("#menuList").sortable('destroy');
						$timeout(function () {
							$scope.search();
						}, 1000);
					} else {
						$scope.msg = result.msg;
					}
				});
			}
		}
		//初始化列表
		$scope.search();
		//初始化条件查询
		$scope.changeMenuType();
		
		$scope.clearSearch = function() {
			$scope.param= { };
            $(".editSortBtn").show();
            $(".saveSortBtn").hide();
            $("#menuBox").hide();
            $scope.param.menuType ='1';
            $.ajax({
                url : $scope.app.project_name+'/menu/read/list',
                data: $scope.param
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageInfo = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
		}
		
		$scope.disableItem = function(id, enable) {
			
		}
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
        };
} ]);